﻿using System.ComponentModel.DataAnnotations;

namespace WebApplication2.Attribute
{
    /// <summary>
    /// 不允许包含空格校验
    /// </summary>
    public class NoSpaceAttribute : ValidationAttribute
    {
        /// <summary>
        /// 重写验证规则
        /// </summary>
        /// <param name="value">属性值</param>
        /// <returns></returns>
        public override bool IsValid(object value)
        {
            bool ret = true;
            if (value is string str)
            {
                ret = !(str.Contains(' '));
            }
            return ret;
        }

        /// <summary>
        /// 重写验证错误信息
        /// </summary>
        /// <param name="name">属性名</param>
        /// <returns></returns>
        public override string FormatErrorMessage(string name)
        {
            return $"{name}不能包含有空格";
        }
    }
}
